package cn.stylefeng.guns.modular.pandian.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.pandian.entity.PandianJihuaDetailed;
import cn.stylefeng.guns.modular.pandian.model.params.PandianJihuaDetailedParam;
import cn.stylefeng.guns.modular.pandian.service.PandianJihuaDetailedService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 盘点明细表控制器
 */
@Controller
@RequestMapping("/pandianJihuaDetailed")
public class PandianJihuaDetailedController extends BaseController {

    private String PREFIX = "/pandian/pandianJihuaDetailed";

    @Autowired
    private PandianJihuaDetailedService pandianJihuaDetailedService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/pandianJihuaDetailed.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/pandianJihuaDetailed_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/pandianJihuaDetailed_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PandianJihuaDetailedParam pandianJihuaDetailedParam) {
        this.pandianJihuaDetailedService.add(pandianJihuaDetailedParam);
        return ResponseData.success();
    }

    @Autowired
    private StorageStockService storageStockService;

    /**
     * 处理接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseData editItem(PandianJihuaDetailedParam pandianJihuaDetailedParam) {
    	User user = userService.getcurrentUser();
        PandianJihuaDetailed detail = this.pandianJihuaDetailedService.getById(pandianJihuaDetailedParam.getId());
        if(detail != null) {
        	if(detail.getInventory().equals("待处理")) {
        		detail.setInventory("已处理");
        		detail.setDealPerson(user.getName());
        		detail.setDealTime(DateUtil.getDateS());
        		ToolUtil.copyProperties(detail, pandianJihuaDetailedParam);
        		this.pandianJihuaDetailedService.update(pandianJihuaDetailedParam);
                boolean flag = storageStockService.correctJhua(pandianJihuaDetailedParam,user);//修改库存
                if (!flag) {
                	return ResponseData.error("库存发生变动");
        		} else {
        	        return ResponseData.success("处理完成");
        		}
        	}
        }
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PandianJihuaDetailedParam pandianJihuaDetailedParam) {
        this.pandianJihuaDetailedService.delete(pandianJihuaDetailedParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PandianJihuaDetailedParam pandianJihuaDetailedParam) {
        PandianJihuaDetailed detail = this.pandianJihuaDetailedService.getById(pandianJihuaDetailedParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PandianJihuaDetailedParam pandianJihuaDetailedParam,String condition ) {
        return this.pandianJihuaDetailedService.findPageBySpec(pandianJihuaDetailedParam,condition);
    }

}


